package com.newyulong.implatform.activity;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

import com.newyulong.implatform.IMPApplication;
import com.newyulong.implatform.R;
import com.newyulong.implatform.adapter.MyGridViewAdapter;
import com.newyulong.implatform.bean.AdJsonBean;
import com.newyulong.implatform.bean.ProductJsonBean;
import com.newyulong.implatform.bean.UserInfoJsonBean;
import com.newyulong.implatform.defines.Defines;
import com.newyulong.implatform.util.AsyncImageLoader;
import com.newyulong.implatform.util.AsyncImageLoader.ImageCallback;
import com.newyulong.implatform.util.CommonsUtil;


public class IndexActivity extends BaseActivity{
	
	
	private TextView tvProductName;
	private TextView tvEndTime;
	private TextView tvIntegral;
	private TextView tvOver;
	private TextView tvFlow;
	private TextView tvWelcome;
	
	private UserInfoJsonBean userInfo;
	private AdJsonBean adInfo;
	private ArrayList<AdJsonBean> adList;
	private ArrayList<ProductJsonBean> productList;
	
	private static final int GET_USER_INFO = 1;
	private static final int GET_AD_INFO = 2;
	private static final int GET_USER_INFO_FAILED = 3;

	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Defines.HTTP_REQUEST_TIMEOUT:
				CommonsUtil.createInfoDialog(IndexActivity.this, "网络异常！");
				break;
			case GET_USER_INFO:
				initUserInfo();
				pd.dismiss();
				break;
			case GET_AD_INFO:
				initAdInfo();
				break;
			default:
				break;
			}
			
		}
	
	};
	private ImageView ivAdImage1;
	private ImageView ivAdImage2;
	private AsyncImageLoader asyncImageLoader;
	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setView(R.layout.index);
		SetTitle("首  页");
		setTitleBar(-1, "", -1, "");
		
		//pd = CommonsUtil.createProgressDialog(this, "正在努力加载数据，请稍后~");
		//pd.show();
		asyncImageLoader = new AsyncImageLoader();
		
		findViews();
		initRes();
		setValues();
		setListeners();
		
		tvProductName = (TextView) findViewById(R.id.tv_product_name);
		Gallery gvLike = (Gallery) findViewById(R.id.gallery);
		gvLike.setAdapter(new MyGridViewAdapter(this));
		gvLike.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				tvProductName.setText("第"+position+"张图片");
			}
		});
	}
	
	
	private void findViews() {
		tvEndTime = (TextView) findViewById(R.id.tv_end_time);
		tvIntegral = (TextView) findViewById(R.id.tv_integral);
		tvOver = (TextView) findViewById(R.id.tv_over);
		tvFlow = (TextView) findViewById(R.id.tv_flow);
		
		tvWelcome = (TextView) findViewById(R.id.tv_welcome);
		
		ivAdImage1 = (ImageView) findViewById(R.id.iv_imgad1);
		ivAdImage2 = (ImageView) findViewById(R.id.iv_imgad2);
		
	}

	//初始化数据
	private void initRes() {
		//getUserInfo();
		//getAdInfo();
		//getProductList();
	}


	private void setValues() {
		// TODO Auto-generated method stub
		
	}


	private void setListeners() {
		// TODO Auto-generated method stub
		
	}
	
	//初始化用户信息
	private void initUserInfo() {
		tvEndTime.setText(userInfo.getEndTime());
		tvIntegral.setText(userInfo.getIntegral());
		tvOver.setText(userInfo.getOver());
		tvFlow.setText(userInfo.getFlow());
		tvWelcome.setText("欢迎");
	};
	//初始化广告图片
	private void initAdInfo() {
		//第一张广告图
		ivAdImage1.setImageBitmap(null);
		Drawable drawable = asyncImageLoader.loadDrawable(
				adList.get(0).getImageUrl(),
				new ImageCallback() {
					public void imageLoaded(Drawable imageDrawable,String imageUrl) {
						ivAdImage1.setImageDrawable(imageDrawable);
					}
				});
		if (drawable == null) {
			ivAdImage1.setImageResource(R.drawable.ic_launcher);
		} else {
			ivAdImage1.setImageDrawable(drawable);
		}
		
		//第二张广告图
		ivAdImage2.setImageBitmap(null);
		Drawable drawable2 = asyncImageLoader.loadDrawable(
				adList.get(1).getImageUrl(),
				new ImageCallback() {
					public void imageLoaded(Drawable imageDrawable,String imageUrl) {
						ivAdImage2.setImageDrawable(imageDrawable);
					}
				});
		if (drawable2 == null) {
			ivAdImage2.setImageResource(R.drawable.ic_launcher);
		} else {
			ivAdImage2.setImageDrawable(drawable2);
		}
	}
	
	
	
	//得到“猜你喜欢”数据
	private void getProductList() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				
				
				
			}
		}).start();
	}
	
	//得到广告数据
	private void getAdInfo() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				adList = IMPApplication.getInstance().adService.getAdsByType("");
			}
		}).start();
	}
	
	//以用户数据
	private void getUserInfo(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					userInfo = IMPApplication.getInstance().userService.getUserInfo();
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendEmptyMessage(Defines.HTTP_REQUEST_TIMEOUT);
				}
				if(userInfo!=null){
					handler.sendEmptyMessage(GET_USER_INFO);
				}
			}
		}).start();
	}
	


	@Override
	protected void HandleTitleBarEvent(int buttonId) {
	}
	
}
